/*jslint vars: true, forin: true, sloppy: true */
/*global angular, probe */
probe.controller("ObjctEditor", function ($scope, $element, $filter, ocDialog, ocAPI) {
	var DI = ocDialog.queryDI($element),
		list = [{name: 'loading', cssPath:'加载中'}],
		share = function () {
			return {
				objectList: $scope.object.list,
				selected: $scope.objectList.selected,
				edit: false,
				refresh: function () {
					$scope.refresh = !$scope.refresh;
				}
			};
		};
	$scope.query = '';//search text
	$scope.object = {};
	$scope.refresh = false;

	//元件库列表控制字段
	$scope.objectList = {
		header: [
			{
				key: 'name',
				text: '键名',
				width: 150
			},
			{
				key: 'cssPath',
				text: '选择器',
				width: 300
			}
		],
		visitFn: function () {
			ocDialog.getNewDI('object/update', angular.extend(share(), {
				edit: true
			}))
			.then(function (AddKeyDI) {
				AddKeyDI.openDialogFrom(DI);
			});
		}
	};
	//元件数据存放的地方

	$scope.search = function () {
		$scope.object.list = $filter('filter')(list, $scope.query);
	};
	$scope.clearSearch = function () {
		$scope.object.list = list;
	};

	$scope.clearSearch();

	ocAPI.use('retrieveObject').by().then(function (data) {
		$scope.object.list = list = angular.fromJson(data.objects);
	});

	$scope.addObject = {
		name: 'object/update',
		share: share
	};
	$scope.deleteObject = function () {
		$scope.object.list.splice($scope.objectList.selected, 1);
	};
	$scope.trashAllObject = {
		title: '警告 - 全部清空',
		message: '您确定要清空整个公共元件库吗？',
		callFn: function () {
			$scope.object.list.length = 0;
		}
	};
});
